package com.sleep.blog.controller;

import com.sleep.blog.common.aop.LogAnnotation;
import com.sleep.blog.common.cache.Cache;
import com.sleep.blog.service.ArticleService;
import com.sleep.blog.vo.Result;
import com.sleep.blog.vo.params.ArticleParam;
import com.sleep.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Sleep-6000
 * @date 2021/9/10
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 首页 文章列表
     *
     * @param pageParams 分页参数
     * @return 文章列表
     */
    @PostMapping
    @LogAnnotation(module = "首页-文章", operation = "查询文章列表")
    @Cache(expire = 2 * 60 * 1000, name = "list_article")
    public Result listArticle(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页 最热文章
     */
    @PostMapping("hot")
    @Cache(expire = 2 * 60 * 1000, name = "hot_article")
    public Result hotArticle() {
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页 最新文章
     */
    @PostMapping("new")
    @Cache(expire = 2 * 60 * 1000, name = "new_article")
    public Result newArticle() {
        int limit = 5;
        return articleService.newArticle(limit);
    }

    /**
     * 首页 文章归并
     */
    @PostMapping("listArchives")
    @Cache(expire = 2 * 60 * 1000, name = "list_archives")
    public Result listArchives() {
        return articleService.listArchives();
    }

    /**
     * 文章详情页
     *
     * @param id 文章 id
     * @return 文章详情
     */
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long id) {
        return articleService.findArticleById(id);
    }

    /**
     * 发布文章
     *
     * @param articleParam articleParam
     * @return articleId
     */
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        return articleService.publish(articleParam);
    }
}
